package com.ima.web.maintenance.controllers;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ima.web.beans.db.Categories;
import com.ima.web.beans.db.SubCategories;
import com.ima.web.beans.db.Users;
import com.ima.web.beans.form.SubCategoriesForm;
import com.ima.web.service.CategoriesService;
import com.ima.web.service.SubCategoriesService;
import com.ima.web.service.UserService;

@Controller
@RequestMapping("/maintenance/subcategories")
public class SubCategoriesMaintenanceController {

	private SubCategoriesService subCatService;
	private CategoriesService categoriesService;
	
	@Autowired
	public void setDependencies(SubCategoriesService subCatService,
							    CategoriesService categoriesService){
		this.subCatService = subCatService;
		this.categoriesService = categoriesService;
	}
	
	@RequestMapping(value="/view")
	public String viewSubCategories(ModelMap model){
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		model.put("approved", subCatService.getApprovedBy(username));
		model.put("notApproved", subCatService.getForProcessing());
		
		return "admin/maintenance/subcategories/viewSubCategories";
	}
	
	@RequestMapping(value="/edit/{id}")
	public String editSubCategories(ModelMap model,@PathVariable(value="id") Integer id){
		SubCategories sc = (SubCategories) subCatService.getById(id);
		
		SubCategoriesForm scf = new SubCategoriesForm();
		scf.setName(sc.getName());
		scf.setDescription(sc.getDescription());
		scf.setSubCategoriesId(sc.getSubCategoriesId());
		scf.setCategoriesId(sc.getCategory().getCategoriesId());
		
		//System.out.println("SubCategories: "+sc.getApprover().getUsername());
		
		if(sc.getApprover()!=null){
			System.out.println("Approved: "+true);
			scf.setApproved(true);
		}
		
		//should not be able to add categories that has not yet been approved		
		model.put("cats",categoriesService.getApproved());
		model.put("subcategory", scf);

		return "admin/maintenance/subcategories/editSubCategories";
	}
	
	@RequestMapping(value="/edit")
	public String add(ModelMap model){
		//should not be able to add categories that has not yet been approved	
		System.out.println("Controller edit subcat approved size: "+categoriesService.getApproved().size());
		model.put("cats",categoriesService.getApproved());
		model.put("subcategory", new SubCategoriesForm());

		return "admin/maintenance/subcategories/editSubCategories";
	}
	
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String save(@ModelAttribute("subcategory") SubCategoriesForm scForm,BindingResult result,ModelMap model){
//		SubCategories sc = new SubCategories();
//		sc.setSubCategoriesId(scForm.getSubCategoriesId());
//		
//		Categories c = categoriesService.getById(scForm.getCategoryId());
//		sc.setName(scForm.getName());
//		sc.setDescription(scForm.getDescription());
//		
//		sc.setCategory(c);
//		sc.setUpdatedDate(new Date());
//		
//		if(scForm.getSubCategoriesId()==null)
//			sc.setCreatedDate(new Date());
//		
//		String username = SecurityContextHolder.getContext().getAuthentication().getName();
//		Users u = userService.getUserData(username);
//		
//		if(scForm.getApproved()){
//			sc.setApprovedDate(new Date());
//			sc.setApprover(u);
//		}
//		
//		subCatService.insert(sc);
		
		subCatService.insert(scForm);
		
		model.put("saved", true);
		model.put("msg", "successfully saved");
		
		return "admin/maintenance/subcategories/editSubCategories";
	}
}
